<script setup lang="ts">
import { computed } from 'vue'
import labelBg from './label-bg.png'
import BlockItem from '@/components/BlockItem/BlockItem.vue'
import EChart from '@/components/EChart/EChart.vue'

const props = withDefaults(
  defineProps<{
    title: string
    unit: string
    data: { name: string, value: [string, number] }[]
  }>(),
  {
    title: '',
    unit: '',
    data: () => [],
  },
)

const defaultNumber = inject<(x: any) => string>('defaultNumber')
const chartOption = computed(() => {
  return {
    tooltip: {
      trigger: 'axis',
      formatter(params: any) {
        params = params[0]
        const date = new Date(params.name)
        return (
          `${date.getFullYear()
          }/${
          date.getMonth() + 1
          }/${
          date.getDate()
          } : ${
          defaultNumber ? defaultNumber(params.value[1]) : params.value[1]
          } ${
          props.unit}`
        )
      },
      axisPointer: {
        type: 'cross',
      },
    },
    textStyle: {
      color: '#899DBF',
    },
    yAxis: {
      type: 'value',
      name: props.unit,
      splitLine: {
        lineStyle: {
          color: 'rgba(28,70,94,0.4)',
        },
      },
      axisLine: {
        show: false,
        lineStyle: {
          color: 'rgba(28,70,94,0.4)',
        },
      },
    },
    xAxis: {
      type: 'time',
      boundaryGap: false,
      axisLine: {
        show: true,
        lineStyle: {
          color: 'rgba(28,70,94,0.8)',
        },
      },
    },
    grid: {
      containLabel: true,
      right: '2%',
      left: '5%',
      top: '20%',
      bottom: '2%',
    },
    visualMap: {
      show: false,
      top: 50,
      right: 10,
      pieces: [
        {
          gt: 0,
          lte: 850,
          color: '#93CE07',
        },
        {
          gt: 850,
          lte: 950,
          color: '#FBDB0F',
        },
        {
          gt: 950,
          lte: 1050,
          color: '#FC7D02',
        },
        {
          gt: 1050,
          lte: 1150,
          color: '#FD0100',
        },
        {
          gt: 1150,
          lte: 1250,
          color: '#AA069F',
        },
        {
          gt: 1250,
          color: '#AC3B2A',
        },
      ],
      outOfRange: {
        color: '#999',
      },
    },
    series: [
      {
        type: 'line',
        symbol: 'circle',
        smooth: true,
        symbolSize: 10,
        showSymbol: false,
        markLine: {
          show: false,
          silent: true,
          animation: false,
          label: {
            color: '#fff',
          },
          lineStyle: {
            color: 'rgb(133,237,165)',
          },
          data: [
            {
              yAxis: 850,
            },
            {
              yAxis: 950,
            },
            {
              yAxis: 1050,
            },
            {
              yAxis: 1150,
            },
            {
              yAxis: 1250,
            },
          ],
        },
        areaStyle: {
          // opacity: 0,
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 1,
                color: 'rgba(36,135,220,0.1)',
              },
              {
                offset: 0,
                color: 'rgba(36,135,220,1)',
              },
            ],
          },
        },
        data: toValue(props.data),
      },
    ],
  }
})

// watch(() => props.data, (val) => {
//   console.log(val[0].name)
// })
</script>

<template>
  <BlockItem :title="title" :head-background-image="labelBg">
    <EChart :option="chartOption" height="20vh" />
  </BlockItem>
</template>
